package org.tuoren.official.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.tuoren.common.core.utils.MapstructUtils;
import org.tuoren.common.mybatis.core.page.TableDataInfo;
import org.tuoren.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.tuoren.official.domain.bo.HistoryBo;
import org.tuoren.official.domain.vo.HistoryVo;
import org.tuoren.official.domain.History;
import org.tuoren.official.mapper.HistoryMapper;
import org.tuoren.official.service.IHistoryService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 发展历程Service业务层处理
 *
 * @author wl
 * @date 2024-11-26
 */
@RequiredArgsConstructor
@Service
public class HistoryServiceImpl implements IHistoryService {

    private final HistoryMapper baseMapper;

    /**
     * 查询发展历程
     *
     * @param id 主键
     * @return 发展历程
     */
    @Override
    public HistoryVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询发展历程列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 发展历程分页列表
     */
    @Override
    public TableDataInfo<HistoryVo> queryPageList(HistoryBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<History> lqw = buildQueryWrapper(bo);
        Page<HistoryVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public TableDataInfo<HistoryVo> frontList() {
        LambdaQueryWrapper<History> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc( History::getHYear);
        return TableDataInfo.build(baseMapper.selectVoList(lqw));
    }
    /**
     * 查询符合条件的发展历程列表
     *
     * @param bo 查询条件
     * @return 发展历程列表
     */
    @Override
    public List<HistoryVo> queryList(HistoryBo bo) {
        LambdaQueryWrapper<History> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<History> buildQueryWrapper(HistoryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<History> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getHYear()), History::getHYear, bo.getHYear());
        lqw.eq(StringUtils.isNotBlank(bo.getPhotoUrl()), History::getPhotoUrl, bo.getPhotoUrl());
        lqw.like(StringUtils.isNotBlank(bo.getHDesc()), History::getHDesc, bo.getHDesc());
        lqw.like(StringUtils.isNotBlank(bo.getTitle()), History::getTitle, bo.getTitle());
        return lqw;
    }

    /**
     * 新增发展历程
     *
     * @param bo 发展历程
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(HistoryBo bo) {
        History add = MapstructUtils.convert(bo, History.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改发展历程
     *
     * @param bo 发展历程
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(HistoryBo bo) {
        History update = MapstructUtils.convert(bo, History.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(History entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除发展历程信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
